// 引入商品详情api请求
import { reqGoodsInfo } from "@/api";
import { reqAddOrUpdateShopCart } from "@/api"
// 引入游客身份模块
import { getUUID } from "@/utils/uuid_token"

export default {
  actions: {
    // 获取产品信息
    async getGoodsInfo ({ commit }, skuId) {
      const result = await reqGoodsInfo(skuId);
      if (result.code == 200) {
        commit('GETGOODSINFO', result.data);
      }
    },
    // 将产品添加到购物车中
    async addOrUpdateShopCart ({ commit }, { skuId, skuNum }) {
      // 加入购物车后返回的结果
      let result = await reqAddOrUpdateShopCart(skuId, skuNum);
      // console.log(result);
      // async函数的返回值是一个Promise
      // 由于返回的data为null，所以不需要state仓库存储数据
      if (result.code == 200) {
        // 加入成功，期约为fullfill
        return '加入购物车成功'
      } else {
        // 加入失败，抛出错误error
        return Promise.reject(new Error('加入购物车失败'))
      }
    }
  },
  mutations: {
    GETGOODSINFO (state, goodsInfo) {
      state.goodsInfo = goodsInfo;
    }
  },
  state: {
    goodsInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
  },
  getters: {
    categoryView (state) {
      return state.goodsInfo.categoryView || {}
    },
    skuInfo (state) {
      return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList (state) {
      return state.goodsInfo.spuSaleAttrList || []
    }
  }
}
